package com.ikangtai.shecare.curve.mpchart;

import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.formatter.AxisValueFormatter;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class BBTXAxisValueFormatter implements AxisValueFormatter {
    @Override
    public String getFormattedValue(float value, AxisBase axis) {
        int deltaDay = (int) (value - BBTLineChart.XAXIS_TODAY);

        return getFormattedXAxisLable(deltaDay);
    }

    @Override
    public int getDecimalDigits() {
        return 0;
    }

    private String getFormattedXAxisLable(int deltaDay){

        if(deltaDay == 0)
            return "今天";

        String ymd = getAfterNDay(deltaDay);
        String month = ymd.split("-")[1];
        String day = ymd.split("-")[2];

        if(day.equals("1"))
            return month + "月";
        else
            //return month + "/" + day;
            return day;
    }

    private String getAfterNDay(int n){
        Calendar c = Calendar.getInstance();
        DateFormat df=new SimpleDateFormat("yyyy-M-d");
        c.setTime(new Date());
        c.add(Calendar.DATE, n);
        Date d2 = c.getTime();
        String s = df.format(d2);
        return s;
    }
}
